import { createRouter, createWebHashHistory } from "vue-router";
import { getisLogin } from "@/api/index.js";

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/index",
    name: "index",
    component: () => import("@/views/index/index.vue"),
  },
  {
    path:"/employmentServices",
    name:'大学生就业服务',
    component: () => import("@/views/EmploymentServices/index.vue")
  },
  {
    path: "/prompt",
    name: "prompt",
    component: () => import("@/views/prompt/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach(async (to, from, next) => {
  // 获取token
  const hostname = window.location.href;
  const token = localStorage.getItem("accessToken");
  if (token) {
    const data = await getisLogin();
    // console.log(data, "6.18");
    if (data.code === 200) {
      if (to.path === "/login" && from.path === "/") {
        next({ path: "/index" });
      } else {
        next();
      }
    }
    next();
  } else {
    if (to.path === "/prompt") {
      next();
    } else if (to.path !== "/login") {
      next({ path: "/login" });
    } else {
      next();
    }
  }
});
export default router;
